<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2016/1/19
 * Time: 13:16
 */

namespace app\controllers;

use app\models\HomeDao;
use Yii;
use app\lib\WXBaseController;
use app\models\BagDao;
use app\models\T_WX_PAY_LOG;
use app\models\weixin\T_WXMALL_USER;
use app\lib\WeixinUtils;
use app\lib\WebService;
use app\lib\Tools;
use app\lib\wxpay\WxPayUnifiedOrder;
use app\lib\wxpay\WxPayTools;
use app\lib\wxpay\WxPayApi;
use app\models\BizDao;

class WxBagController extends WXBaseController
{


    public function beforeAction($action)
    {
        $userinfo = WeixinUtils::weixin_userinfo_filter();
        if ($userinfo == null) {
            echo "验证失败";
            return false;
        }
        return true;
    }

    //首页
    public function actionHome()
    {
        return $this->render("home");
    }

    //产品列表
    public function actionGoodsList()
    {
        $session = Yii::$app->session;
        $userid = $session->get(static::sessionKey)->userid;
        $TOTALNUM = BagDao::calc_dorabag_cart_order_TOTALNUM($userid);
        return $this->render("goods-list", ['TOTALNUM' => $TOTALNUM]);
    }

    //查询产品
    public function actionGetGoodsListHandle()
    {
        $session = Yii::$app->session;
        $openid = $session->get(static::sessionKey)->openid;
        $msg = "";
        $flag = 0;
        $request = Yii::$app->request;
        $start = $request->post("start");
        $pagesize = $request->post("pagesize");
        $category = $request->post("category");
        $order = $request->post("order");
        $filter = $request->post("filter");

        $goodsList = BagDao::get_GoodsList($start, $pagesize, $category, $order, $filter, $openid);
        $flag = 1;
        echo json_encode(['msg' => $msg, 'flag' => $flag, 'goodsList' => $goodsList]);
    }

    public function actionGoodsCategory()
    {
        $session = Yii::$app->session;
        $openid = $session->get(static::sessionKey)->openid;
        $categoryList = BagDao::get_Category($openid);
        return $this->render("goods-category", ['categoryList' => $categoryList]);
    }

    //产品筛选
    public function actionGoodsQuery($category = 0)
    {
        return $this->render("goods-query", ['category' => $category]);
    }

    //产品详情
    public function actionGoodsDetail($goods_code)
    {
        $session = Yii::$app->session;
        $userid = $session->get(static::sessionKey)->userid;
        $re = BagDao::get_Goods($goods_code);
        if ($re == false) {
            echo "没有这个产品";
            exit;
        }
        $TOTALNUM = BagDao::calc_dorabag_cart_order_TOTALNUM($userid);
        return $this->render("goods-detail", ['goods' => $re['goods'], 'imgList' => $re['imgList'], 'TOTALNUM' => $TOTALNUM]);
    }

    //加入购物车 add-goods-to-cart-handle
    public function actionAddGoodsToCartHandle()
    {
        $session = Yii::$app->session;
        $msg = "";
        $flag = 0;
        $request = Yii::$app->request;

        //$userid,$goods_code
        $userid = $session->get(static::sessionKey)->userid;
        $goods_code = $request->post("goods_code");

        $re = BagDao::add_dorabag_cart($userid, $goods_code);
        if ($re == '1') {
            $flag = 1;
            $TOTALNUM = BagDao::calc_dorabag_cart_order_TOTALNUM($userid);
        } else {
            $msg = $re;
            $TOTALNUM = null;
        }
        echo json_encode(['msg' => $msg, 'flag' => $flag, 'TOTALNUM' => $TOTALNUM]);
    }

    //增减购物车某产品数量 inc-goods-to-cart-handle
    public function actionIncGoodsToCartHandle()
    {
        $session = Yii::$app->session;
        $msg = "";
        $flag = 0;
        $request = Yii::$app->request;

        //$userid,$goods_code
        $userid = $session->get(static::sessionKey)->userid;
        $goods_code = $request->post("goods_code");
        $addnum = $request->post("addnum");
        $updateCount = BagDao::inc_dorabag_cart_num($userid, $goods_code, $addnum);
        if ($updateCount == 0) {
            $msg = "增减数量失败";
            echo json_encode(['msg' => $msg, 'flag' => $flag]);
        } else {
            $flag = 1;
            $TOTALPRICE = floatval(BagDao::calc_dorabag_cart_order_TOTALPRICE($userid));
            $TOTALNUM = BagDao::calc_dorabag_cart_order_TOTALNUM($userid);
            echo json_encode(['msg' => $msg, 'flag' => $flag, 'TOTALPRICE' => $TOTALPRICE, 'TOTALNUM' => $TOTALNUM]);


        }

    }

    //删除购物车某产品 del-goods-to-cart-handle
    public function actionDelGoodsToCartHandle()
    {
        $session = Yii::$app->session;
        $msg = "";
        $flag = 0;
        $request = Yii::$app->request;

        //$userid,$goods_code
        $userid = $session->get(static::sessionKey)->userid;
        $goods_code = $request->post("goods_code");

        $updateCount = BagDao::delete_dorabag_cart($userid, $goods_code);
        if ($updateCount == 0) {
            $msg = "删除失败";
        } else {
            $flag = 1;
        }
        echo json_encode(['msg' => $msg, 'flag' => $flag]);
    }

    //  购物车
    public function actionShoppingCart()
    {

        return $this->render("shopping-cart");
    }

    //获取购物车信息 get-goods-cart-handle
    public function actionGetGoodsCartHandle()
    {
        $session = Yii::$app->session;
        $msg = "";
        $flag = 1;
        $request = Yii::$app->request;

        //$userid,$goods_code
        $userid = $session->get(static::sessionKey)->userid;


        $cartList = BagDao::get_shopping_cartList($userid);
        $TOTALPRICE = floatval(BagDao::calc_dorabag_cart_order_TOTALPRICE($userid));

        $TOTALNUM = BagDao::calc_dorabag_cart_order_TOTALNUM($userid);
        echo json_encode(['msg' => $msg, 'flag' => $flag, 'cartList' => $cartList, 'TOTALPRICE' => $TOTALPRICE, 'TOTALNUM' => $TOTALNUM]);
    }

    const fa_piao_price = 8;

    //生成订单 create-cart-to-order-handle
    public function actionCreateCartToOrderHandle()
    {

        $session = Yii::$app->session;
        $msg = "";
        $flag = 0;
        $request = Yii::$app->request;

        $userid = $session->get(static::sessionKey)->userid;
        //计算总价
        $TOTALPRICE = floatval(BagDao::calc_dorabag_cart_order_TOTALPRICE($userid));
        if ($TOTALPRICE == 0) {
            $msg = "订单总金额不能为0";
        } else {

            //生成订单
            $order_id = BagDao::P_WXMALL_CREATE_DORABAG_ORDER($userid, $TOTALPRICE);
            if ($order_id == '0') {
                $msg = "生成订单失败";
            } else {
                $flag = 1;
                echo json_encode(['msg' => $msg, 'flag' => $flag, 'order_id' => $order_id]);
                exit;
            }
        }

        echo json_encode(['msg' => $msg, 'flag' => $flag]);
    }

    //校验订单并返回订单信息
    public static function ___verifyOrder($code, $user_id)
    {

        $orderInfo = BagDao::getOrderInfoByOrderCode($code, $user_id);
        if (count($orderInfo) == 0) {
            echo "订单不存在";
            exit;
        }
        return $orderInfo = $orderInfo[0];
    }

    ////点击我的送礼记录跳转 查看订单导航  code 订单编号  view-order-nav
    public function actionViewOrderNav($code)
    {
        $session = Yii::$app->session;
        $user_id = $session->get(static::sessionKey)->userid;

        $orderInfo = static::___verifyOrder($code, $user_id);
        $STATUS = $orderInfo['STATUS'];
//        1：已购买未支付
//        2：已支付
//        3：已失效

        if ($STATUS == '1') {
            return $this->redirect(["wx-bag/order-form", 'code' => $code]);
        } else {
            if ($STATUS == '2') {
                //已支付
                return $this->redirect(["wx-bag/send-gift-detail", 'code' => $code]);
                exit;
            } else {
                echo "已失效;STATUS:" . $STATUS;
                exit;
            }
        }
    }

    //订单确认  code 订单编号 ps:为了合并到统一的支付路径下
    public function actionOrderForm($code)
    {
        return $this->redirect(["wx-pay/wx-bag-order-form", 'code' => $code]);
    }

    const defalut_blessing = '我在朵拉盒为您选了一份朵拉礼包，祝您万事如意。';

    //产生微信支付订单 pay-handle
    public function actionPayHandle()
    {
        $msg = "";
        $flag = 0;
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $code = $request->post("code");
        $current_coin = $request->post("current_coin");
        $BLESSING = $request->post("blessing");
        if (trim($BLESSING) == '') {
            $BLESSING = static::defalut_blessing;
        }

        $user_id = $session->get(static::sessionKey)->userid;
        $nickname = $session->get(static::sessionKey)->nickname;
        $orderInfo = BagDao::getOrderInfoByOrderCode($code, $user_id);
        if (count($orderInfo) == 0) {
            $msg = "订单不存在";

        } else {
            $orderInfo = $orderInfo[0];


            //添加祝福语
            $re = BagDao::add_dorabag_cart_order_BLESSING($code, $user_id, $BLESSING);
            if ($re == 0) {
                echo json_encode(['msg' => '添加祝福语失败', 'flag' => 0,]);
                exit;
            }
            //计算价格
            $result = BizDao::P_WXMALL_CALC_PRICE($user_id, $current_coin, $code);
            if ($result['RESULTS'] != 1) {
                echo json_encode(['msg' => '生成支付订单失败:' . $result['RESULTS'], 'flag' => 0,]);
                exit;
            }
            $price = $result['realPrice'];
            $price = floatval($price) * 100;
            //附加数据
            $attach = '';
            if ($current_coin != '0') {
                $attach = "current_coin:{$current_coin};";
            }
            //加入订单号
            $attach = $attach . "order_no:{$code};";
            $random_order = Tools::build_order_no('bag_');
            if (YII_ENV == 'dev' && $price > 0) {

                echo json_encode(['msg' => '测试单证产生完毕', 'flag' => 0, 'testcode' => $code, 'price' => $price]);
                exit;
            }
            $openid = $session->get(static::sessionKey)->openid;
            if ($price > 0) {


                //产生微信支付订单
                // //---------begin---------生成支付订单


                $input = new WxPayUnifiedOrder();
                $input->SetBody($nickname . '的朵拉礼包');
                $input->SetOut_trade_no($random_order);
                $input->SetTotal_fee($price);
                $input->SetTime_start(date("YmdHis"));
                $input->SetTime_expire(date("YmdHis", time() + 600));
                $input->SetNotify_url(PAYURL);
                $input->SetTrade_type("JSAPI");
                $input->SetOpenid($openid);
                $input->SetAttach($attach);
                $order = WxPayApi::unifiedOrder($input);

                //return_code 和result_code
                if ($order['return_code'] == 'SUCCESS' && $order['result_code'] == 'SUCCESS') {
                    $prepay_id = $order['prepay_id'];
                    $jsApiParameters = WxPayTools::GetJsApiParameters($order);
                    $flag = 1;  //生成成功
                    echo json_encode(['msg' => $msg, 'flag' => $flag, 'code' => $code, 'jsApiParameters' => $jsApiParameters, 'price' => $price]);
                    exit;
                } else {
                    $msg = '下支付订单错误：' . $order['return_msg'];

                }
                ///---------------end
            } else {//价格为0

                $re = T_WX_PAY_LOG::P_WXMALL_PAY_DORABAG_ORDER('SUCCESS', ''
                    , $openid, 0, $random_order, 'price is zero', date("YmdHis")
                    , $attach);
                if ($re != '1') {
                    $flag = 0;
                    $msg = 'pay order error!';
                    echo json_encode(['msg' => $msg, 'flag' => $flag, 'code' => $code]);
                    exit;
                }

                $flag = 1;  //生成成功
                echo json_encode(['msg' => $msg, 'flag' => $flag, 'code' => $code, 'price' => $price]);
                exit;
            }
        }
        echo json_encode(['msg' => $msg, 'flag' => $flag, 'code' => $code]);
    }

    public function actionPaySuccess($order_code)
    {
        $session = Yii::$app->session;
        $user_id = $session->get(static::sessionKey)->userid;
        $user = T_WXMALL_USER::getUserByUserid($user_id);
        $user = $user[0];
        return $this->render("pay-success", ['order_code' => $order_code, 'user' => $user]);
    }

    //领取礼物   gift-receive?code=WP_0000_2016_1401
    public function actionGiftReceive($order_code)
    {
        $session = Yii::$app->session;
        $user_id = $session->get(static::sessionKey)->userid;
        $bagInfo = static::____getBagInfo($order_code);
        if ($bagInfo == false) {
            echo "没有这个礼包";
            exit;
        }


        return $this->render("gift-receive", array_merge($bagInfo, []));
    }

    //获取礼包领取情况  get-gift-receive-info-handle
    public function actionGetGiftReceiveInfoHandle()
    {

        $session = Yii::$app->session;
        $msg = "";
        $flag = 0;
        $request = Yii::$app->request;
        $order_code = $request->post('order_code');
        $userid = $session->get(static::sessionKey)->userid;

        $bagInfo = static::____getBagInfo($order_code);
        if ($bagInfo == false) {
            $msg = "没有这个礼包";
            echo json_encode(['msg' => $msg, 'flag' => $flag,]);
            exit;
        } else {

            $flag = 1;
            $re = BagDao::get_GiftInfoByOrderCode($order_code, $userid);
            $isReceive = 0;
            if (count($re) > 0) {
                $isReceive = 1;
                //查询领取的产品
                $goods = BagDao::get_bagGoods_receive_GOODSINFO($order_code, $userid);
                if (count($goods) > 0)
                    $goods = $goods[0];
            }
            echo json_encode(array_merge($bagInfo, ['msg' => $msg, 'flag' => $flag, 'isReceive' => $isReceive, 'goods' => isset($goods) ? $goods : null]));
            exit;
        }


    }

    //领取礼物 gift-receive-handle
    public function actionGiftReceiveHandle()
    {

        $session = Yii::$app->session;
        $msg = "";
        $flag = 0;
        $request = Yii::$app->request;

        $userid = $session->get(static::sessionKey)->userid;

        $code = $request->post("code");

        $re = BagDao::P_RECEIVE_DORABAG($code, $userid);

        if ($re == '1' || $re == '2') {
            $flag = 1;
            if ($re == '1') {
                //查询领取的产品
                $goods = BagDao::get_bagGoods_receive_GOODSINFO($code, $userid);
                if (count($goods) > 0)
                    $goods = $goods[0];
            }
        } else {
            $msg = $re;
        }
        echo json_encode(['msg' => $msg, 'flag' => $flag, 're' => $re, 'goods' => isset($goods) ? $goods : null]);
    }

    ////点击我的收礼记录跳转 查看礼物导航  code_id t_wxmall_dorabag_order_goods.id  view-gift-nav
    public function actionViewGiftNav($code_id)
    {
        $session = Yii::$app->session;
        $user_id = $session->get(static::sessionKey)->userid;

        $GiftInfo = BagDao::get_GiftInfoByCodeId($code_id, $user_id);
        if (count($GiftInfo) == 0) {
            echo "没有这个礼品";
            exit;
        }
        $GiftInfo = $GiftInfo[0];
        $STATUS = $GiftInfo['STATUS'];
        $code = $GiftInfo['BAG_ORDER_ID'];
//        $orderInfo=static::___verifyOrder($code,$user_id);
//        $STATUS=$orderInfo['STATUS'];
//        0：未领取
//1：已领取，未兑换
//2：已兑换
//3：未兑换，已经转换为积分

        if ($STATUS == '1') {
            return $this->redirect(["wx-bag/gift-receive-detail", 'code' => $code]);
        } else {
            if ($STATUS == '2') {
                //已兑换

                return $this->redirect(["wx-bag/gift-exchange-detail", 'code' => $code]);
                exit;

            }
            if ($STATUS == '3') {
//                echo "已兑换成积分";
                return $this->redirect(["wx-bag/gift-exchange-detail", 'code' => $code]);
                exit;
            } else {
                echo "已失效;STATUS:" . $STATUS;
                exit;
            }
        }
    }

    public function ___goods_CL_ToFlag($GOODS_CL)
    {
        $flag = 0;//普通商品
        if ($GOODS_CL == '朵拉活动虚拟产品') {//七龙珠
            $flag = 1;
        }
        if ($GOODS_CL == '第三方虚拟产品') {//三方卷
            $flag = 2;
        }
        if ($GOODS_CL == '朵拉币') {//朵拉币
            $flag = 3;
        }
        return $flag;
    }

    //领取礼物详情
    public function actionGiftReceiveDetail($code)
    {
        $session = Yii::$app->session;
        $userid = $session->get(static::sessionKey)->userid;
        $bagInfo = BagDao::get_bagInfo($code);
        if (count($bagInfo) == 0) {
            echo "没有这个礼包";
            exit;
        }
        $bagInfo = $bagInfo[0];
        $goods = BagDao::get_bagGoods_receive_GOODSINFO($code, $userid);
        if (count($goods) == 0) {
            echo "查询商品失败";
            exit;
        }
        $goods = $goods[0];
        $GOODS_CL_flag = static::___goods_CL_ToFlag($goods['GOODS_CL']);


        return $this->render("gift-receive-detail", ['bagInfo' => $bagInfo, 'goods' => $goods, 'GOODS_CL_flag' => $GOODS_CL_flag, 'code' => $code]);
    }

    //兑换礼物
    public function actionCollectGoodsMessage($code, $address_id = '')
    {
        $session = Yii::$app->session;
        $userid = $session->get(static::sessionKey)->userid;
        $goods = BagDao::get_bagGoods_receive_GOODSINFO($code, $userid);
        if (count($goods) == 0) {
            echo "查询商品失败";
            exit;
        }
        $goods = $goods[0];
        $IMG_INFO = BagDao::get_GOODS_IMG_INFO($goods['GOODS_CODE']);
        //获取地址
        $address = HomeDao::getUserAddress($address_id, static::getSessionUserid());
        return $this->render("collect-goods-message", ['goods' => $goods, 'IMG_INFO' => $IMG_INFO, 'code' => $code,
            'address' => $address,
        ]);
    }

    //json 提交订单---普通商品 submit-order-handle
    public function actionSubmitOrderHandle()
    {
        $session = Yii::$app->session;
        $request = Yii::$app->request;


        $code_id = $request->post("code_id");
        $RECEIVERNAME = $request->post("RECEIVERNAME");
        $RECEIVERPHONE = $request->post("RECEIVERPHONE");
        $RECEIVERADDRESS = $request->post("RECEIVERADDRESS");
        $CREMARK = $request->post("CREMARK");
        $sheng = $request->post("sheng");
        $shi = $request->post("shi");
        $qu = $request->post("qu");

        $msg = "";
        $flag = 0;
        $userid = $session->get(static::sessionKey)->userid;

        $giftInfo = BagDao::get_GiftInfoByCodeId_status($code_id, $userid, 1);
        if (count($giftInfo) == 0) {
            $msg = "不能兑换";
        } else {
            $giftInfo = $giftInfo[0];
            //判断是否是普通商品
            $GOODS_CL_flag = static::___goods_CL_ToFlag($giftInfo['GOODS_CL']);
            if ($GOODS_CL_flag != '0') {
                $msg = "不能兑换不是普通商品";
                echo json_encode(['msg' => $msg, 'flag' => $flag]);
                exit;
            }
            //判断库存
            $stock = WebService::nc_getGiftStock($giftInfo['GOODS_ID']);
            if($stock<=0) {
                echo json_encode(['msg' => '库存不足', 'flag' => 0]);
                exit;
            }
            $re = BagDao::submit_goods_exchange_order($code_id, $userid, $RECEIVERNAME, $RECEIVERPHONE, $RECEIVERADDRESS, $sheng, $shi, $qu, '', 0);
            if ($re == 0) {
                $msg = "兑换填写信息失败";
            } else {
                /*public 'status' => string '0' (length=1)
             public 'info' => string '收货人不能为空!' (length=22)
             public 'data' => string '' (length=0)*/
                $re = WebService::nc_setWxOrder($giftInfo['ID'], $RECEIVERNAME,
                    $sheng, $shi, $qu,
                    $RECEIVERADDRESS, $RECEIVERPHONE, "", "", $giftInfo['PRICE'], $giftInfo['GOODS_ID'], $giftInfo['GOODS_NAME'], "1", $giftInfo['PRICE']);
//            var_dump($re);
//                exit;
                BagDao::setNC_SYNSTATUS($code_id, $re->status);
                $flag = 1;
            }
        }
        echo json_encode(['msg' => $msg, 'flag' => $flag]);
        exit;
    }

    //json 提交订单---三方劵
    public function actionSubmitOrderTicketHandle()
    {
        $session = Yii::$app->session;
        $request = Yii::$app->request;


        $code_id = $request->post("code_id");


        $msg = "";
        $flag = 0;
        $userid = $session->get(static::sessionKey)->userid;

        $giftInfo = BagDao::get_GiftInfoByCodeId_status($code_id, $userid, 1);

        if (count($giftInfo) == 0) {
            $msg = "不能兑换";
        } else {
            $giftInfo = $giftInfo[0];
            //判断商品类型
            $GOODS_CL_flag = static::___goods_CL_ToFlag($giftInfo['GOODS_CL']);
            if ($GOODS_CL_flag != '2') {
                $msg = "不能兑换不是三方券";
                echo json_encode(['msg' => $msg, 'flag' => $flag]);
                exit;
            }
            //判断库存
            $stock = WebService::nc_getGiftStock($giftInfo['GOODS_ID']);
            if($stock<=0) {
                echo json_encode(['msg' => '库存不足', 'flag' => 0]);
                exit;
            }
//            {"status":"1","info":"生成出库任务以及订单成功","data":{"goods_code":"MDXK0002","xuliehao":"61631958",'enddate':'2001-2-2'}

            $re = WebService::nc_setWxOrder($giftInfo['ID'], "",
                "", "", "",
                "", "", "", "", $giftInfo['PRICE'], $giftInfo['GOODS_ID'], $giftInfo['GOODS_NAME'], "1", $giftInfo['PRICE']);

            if ($re->status == '1') {
                $xuliehao = $re->data->virtual_volume[0]->xuliehao;
                $enddate = $re->data->virtual_volume[0]->enddate;
                $remark = "兑换码：$xuliehao <br>有效期：$enddate";
                $re = BagDao::submit_goods_exchange_order($code_id, $userid, "", "", "", "", "", "", $remark, 1);
                if ($re == 0) {
                    $msg = "兑换填写信息失败";
                } else {
                    $flag = 1;
                }
            } else {
                $msg = $re->info;
            }
        }
        echo json_encode(['msg' => $msg, 'flag' => $flag]);
    }

    //礼物兑换详情
    public function actionGiftExchangeDetail($code)
    {
        $session = Yii::$app->session;
        $userid = $session->get(static::sessionKey)->userid;

        $bagInfo = BagDao::get_bagInfo($code);
        if (count($bagInfo) == 0) {
            echo "没有这个礼包";
            exit;
        }
        $bagInfo = $bagInfo[0];

        $goods = BagDao::get_bagGoods_receive_GOODSINFO($code, $userid);
        if (count($goods) == 0) {
            echo "查询商品失败";
            exit;
        }
        $goods = $goods[0];

        $GOODS_CL_flag = static::___goods_CL_ToFlag($goods['GOODS_CL']);
        $code_id = $goods['CODE_ID'];
        //order_goods
        $GiftInfo = BagDao::get_GiftInfoByCodeId($code_id, $userid);
        if (count($GiftInfo) == 0) {
            echo "查询订单商品失败";
            exit;
        }
        $GiftInfo = $GiftInfo[0];
        $STATUS = $GiftInfo['STATUS'];

        if ($STATUS == '3') {
            $DORACOIN = HomeDao::get_DORACOIN($code, $GiftInfo['ID'], '2', $userid);
            if (count($DORACOIN) == 0) {
                echo "查询DORACOIN失败";
                exit;
            }
            $DORACOIN = $DORACOIN[0];
        }

        return $this->render("gift-exchange-detail", ['goods' => $goods, 'STATUS' => $STATUS
            , 'bagInfo' => $bagInfo, 'GiftInfo' => $GiftInfo, 'GOODS_CL_flag' => $GOODS_CL_flag
            , 'DORACOIN' => isset($DORACOIN) ? $DORACOIN : null]);
    }

    //获取礼包信息
    private function ____getBagInfo($code)
    {

        $bagInfo = BagDao::get_bagInfo($code);
        if (count($bagInfo) == 0) {
            // echo "没有这个礼包";
            return false;
        }
        $bagInfo = $bagInfo[0];
        $count = BagDao::get_bagGoodsCount($code);
        $receive_count = BagDao::get_bagGoods_receive_Count($code);
        $receive_infoList = BagDao::get_bagGoods_receive_infoList($code);
        return ['bagInfo' => $bagInfo, 'count' => $count, 'receive_count' => $receive_count, 'receive_infoList' => $receive_infoList];
    }

    // 送礼记录详情
    public function actionSendGiftDetail($code)
    {
        $bagInfo = static::____getBagInfo($code);
        if ($bagInfo == false) {
            echo "没有这个礼包";
            exit;
        }
        return $this->render("send-gift-detail", array_merge($bagInfo, []));

    }

    //送礼记录--查看礼包详情
    public function actionSendGiftDetailView($order_code)
    {
        $session = Yii::$app->session;
        $user_id = $session->get(static::sessionKey)->userid;
        $orderInfo = WxBagController::___verifyOrder($order_code, $user_id);
        $STATUS = $orderInfo['STATUS'];
//        1：已购买未支付
//        2：已支付
//        3：已失效

        if ($STATUS != '2') {
            if ($STATUS == '1') {
                //已支付
                echo "已购买未支付;STATUS:" . $STATUS;
                exit;
            } else {
                echo "已失效;STATUS:" . $STATUS;
                exit;
            }
        }

        $GoodsList = BagDao::getOrderGoodsListByOrderCode($order_code);
        return $this->render("send-gift-detail-view", ['orderInfo' => $orderInfo, 'GoodsList' => $GoodsList]);
    }

    // 定制礼包商品列表
    public function actionGoodsCustomList($bag_id)
    {
        $goodsList = BagDao::get_BagGoodsList($bag_id);
        $totalPrice = 0;
        foreach ($goodsList as $goods) {
            $totalPrice += $goods["PRICE"] * $goods["GOODS_SL"];
        }
        return $this->render("goods-custom-list", ['goods_list' => $goodsList, 'total_price' => $totalPrice, 'bag_id' => $bag_id]);
    }

    //定制礼包处理 submit-goods-custom-handle
    public function actionSubmitGoodsCustomHandle()
    {
        $session = Yii::$app->session;
        $msg = "";
        $flag = 0;
        $request = Yii::$app->request;

        $userid = $session->get(static::sessionKey)->userid;
//        $DORABAGID,$TOTALPRICE
        $DORABAGID = $request->post('DORABAGID');
        $goodsList = BagDao::get_BagGoodsList($DORABAGID);
        $totalPrice = 0;
        foreach ($goodsList as $goods) {
            $totalPrice += $goods["PRICE"] * $goods["GOODS_SL"];
        }

        $re = BagDao::P_WXMALL_CUSTOM_DORABAG_ORDER($userid, $DORABAGID, $totalPrice);
        if ($re != '0') {
            $flag = 1;
        }
        echo json_encode(['msg' => $msg, 'flag' => $flag, 're' => $re]);
    }
}